<?php
// +----------------------------------------------------------------------
// | 有客商城 [ 山西幺九网络科技有限公司 版权所有 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.yuuck.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 燕南天 <81020302@qq.com> 2018/7/15/015 9:26
// +----------------------------------------------------------------------

namespace app\admin\service;

use app\admin\model\Admin;

class AdminService
{
    protected $model;
    public function __construct()
    {
        $this->model = new Admin();
    }

    /**
     * 登陆
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login($data){
        $user = $this->model->where(['user_name'=>$data['username']])->field('admin_id,user_name,pwd,avatar')->find();
        if($user){
            if($user['pwd'] == md5($data['password'])){
                //登陆信息
                session('user_name', $user['user_name']);
                session('aid', $user['admin_id']);
                $avatar = $user['avatar'] == ''?'/static/admin/images/0.jpg': $user['avatar'];
                session('avatar', $avatar);

                return ['code' => 1, 'msg' => '登录成功!']; //信息正确
            }else{
                return ['code' => 0, 'msg' => '用户名或者密码错误，重新输入!']; //密码错误
            }
        }else{
            return ['code' => 0, 'msg' => '用户不存在!']; //用户不存在
        }
    }

    public function check($code){
        return captcha_check($code);
    }

}